<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
    <el-form-item
        label="用户ID"
        prop="uid"
      >
        <el-input
          v-model="dataForm.uid"
          required 
          placeholder="用户ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="昵称"
        prop="name"
      >
        <el-input
          v-model="dataForm.name"
          required 
          placeholder="昵称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="手机号"
        prop="phone"
      >
        <el-input
          v-model="dataForm.phone"
          required 
          placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="邮箱"
        prop="postCode"
      >
        <el-input
          v-model="dataForm.postCode"
          required 
          placeholder="邮编"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="省"
        prop="province"
      >
        <el-input
          v-model="dataForm.province"
          required 
          placeholder="省"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="市"
        prop="city"
      >
        <el-input
          v-model="dataForm.city"
          required 
          placeholder="市"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="区"
        prop="region"
      >
        <el-input
          v-model="dataForm.region"
          required 
          placeholder="区"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="详细地址"
        prop="detailAddress"
      >
        <el-input
          v-model="dataForm.detailAddress"
          required 
          placeholder="详细地址"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="是否为默认地址"
        prop="defaultStatus"
      >
        <template>
          <el-radio-group v-model="dataForm.defaultStatus">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        uid: '',
        name: '',
        phone: '',
        postCode: '',
        province: '',
        city: '',
        region: '',
        detailAddress: '',
        defaultStatus: 0
      },
      dataRule: {
        uid: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '昵称不能为空', trigger: 'blur' }],
        phone: [{ required: true, message: '手机号不能为空', trigger: 'blur' }],
        postCode: [{ required: true, message: '邮编不能为空', trigger: 'blur' }],
        province: [{ required: true, message: '“省份”不能为空', trigger: 'blur' }],
        city: [{ required: true, message: '“市”不能为空', trigger: 'blur' }],
        region: [{ required: true, message: '“区”不能为空', trigger: 'blur' }],
        detailAddress: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
        defaultStatus: [{required: true, message: '是否为默认地址[0-不是默认；1-是默认]不能为空', trigger: 'blur'}]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/user/receiveaddress/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.uid = data.receiveAddress.uid
              this.dataForm.name = data.receiveAddress.name
              this.dataForm.phone = data.receiveAddress.phone
              this.dataForm.postCode = data.receiveAddress.postCode
              this.dataForm.province = data.receiveAddress.province
              this.dataForm.city = data.receiveAddress.city
              this.dataForm.region = data.receiveAddress.region
              this.dataForm.detailAddress = data.receiveAddress.detailAddress
              this.dataForm.defaultStatus = data.receiveAddress.defaultStatus
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/receiveaddress/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'uid': this.dataForm.uid,
              'name': this.dataForm.name,
              'phone': this.dataForm.phone,
              'postCode': this.dataForm.postCode,
              'province': this.dataForm.province,
              'city': this.dataForm.city,
              'region': this.dataForm.region,
              'detailAddress': this.dataForm.detailAddress,
              'defaultStatus': this.dataForm.defaultStatus
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
